package com.scu.bank.web;
import com.scu.bank.core.jo.Result;
import com.scu.bank.core.jo.ResultGenerator;
import com.scu.bank.model.pojo.OrgInfo;
import com.scu.bank.model.vo.TreeNode;
import com.scu.bank.service.OrgInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
* Created by pmeng on 2021/01/05.
*/
@RestController
@RequestMapping("/org/info")
public class OrgInfoController {
    @Autowired
    private OrgInfoService orgInfoService;

    @PostMapping("/add")
    public Result add(@RequestParam(required = true) String orgName,@RequestParam(required = true) String orgId) {
        orgInfoService.addOrgInfo(orgName,orgId);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(String id) {
        orgInfoService.delete(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestParam(required = true) String orgName,@RequestParam(required = true) String orgId) {
        orgInfoService.updateRogInfo(orgName,orgId);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageInfo<OrgInfo> pageInfo = orgInfoService.list(page, size);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/getTreeByParent")
    public Result getTreeByParent(String pId){
        TreeNode orgTree = orgInfoService.getTreeByParent(pId);
        return ResultGenerator.genSuccessResult(Arrays.asList(orgTree));
    }
}
